<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * Created on 2012-7-4
 *
 * 可以直接在类或视图中调用的公共函数，与核心Common不同.但也是直接调用。
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

class common{
		/*
		 *根据选项名称获取用户选项值
		 */
		function _get_setting($item=''){
			$CI = &get_instance();
			$CI->load->model('settings_m');
			return $item != '' ? $CI->settings_m->get($item) : false;
		}
		function _get_where_from($need,$where,$from){
			$CI = &get_instance();
			$CI->db->select($need);
			$CI->db->where($where);
			$CI->db->from($from);
			return $result = $CI->db->get();

		}
		/*
		 * 根据类树生成连接
		 * @param @category_tree
		 * @param @rediect 连接
		 */
		 function _get_category_name($category_tree,$redirect=''){
			$data ='';
			$split = '_';

			if($category_tree){
				$categoryids = explode($split,$category_tree);
			}
			if(is_array($categoryids)){
				$last_link = array_pop($categoryids);
			}
			while($categoryids)
			{
				$categoryid = array_shift($categoryids);
				$category= self::_get_category_byid($categoryid);
				$data .= anchor($redirect.$category[0]->alias,$category[0]->categoryname.'->');
			}
			$category = self::_get_category_byid($last_link);
			$data .= anchor($redirect.$category[0]->alias,$category[0]->categoryname);
			return $data;
		}


		/*
		* 根据id获取分类名称和分类别名
		* */
		function _get_category_byid($categoryid){
			$CI = &get_instance();
			$CI->load->model('category_m');
			$query = $CI->category_m->get_where(array('categoryname','alias'),array('cid'=>$categoryid))->result();
			return $query;
		}
		/*
 		* 根据别名获取分类id
 		*/
		function _get_id_by_alias($alias){
			$CI = &get_instance();
			$CI->load->model('category_m');
			$category_id = $CI->category_m->get_id_by_alias($alias)->result();
			return $category_id;
		}
		/*
		 * 根据别名获取分类名称
		 */
		 function _get_categoryname_by_alias($alias){
		 	$CI = &get_instance();
			$CI->load->model('category_m');
			$categoryname = $CI->category_m->get_categoryname_by_alias($alias)->result();
			return $categoryname;
		 }
		/*
		 * 根据用户名获取用户id
		 */
		function _get_id_by_username($username){
			if($username=='')return false;
			$CI = &get_instance();
			$CI->load->model('user_m');
			$user_id = $CI->user_m->get_id_by_username($username)->result();
			return $user_id;
		}
		/*
		 * 获取uri字段
		 */
		function _get_segment($n){
			$CI = &get_instance();
			return $CI->uri->segment($n)? $CI->uri->segment($n) : false;
		}
		/*
		 * 生成action_list session
		 */
 		function _create_actionlist_session($username){
			$CI = &get_instance();
			$userid = common::_get_id_by_username($username);
			$userid = $userid[0]->userid;
			$CI->load->library('role');
			$actionlist = $CI->role->get_action_list($userid);
			$CI->session->set_userdata('actionlist',$actionlist);
 		}
 		function _get_actions_by_roleid($roleid){
 			$CI =  &get_instance();
 		}
 		/*
 		 * 根据actionlist生成menu文件
 		 */
 		function _create_menu_files($actionlist){
			$CI = &get_instance();
			$CI->load->library('role');
			$CI->role->create_menu_files($actionlist);
 		}
		/*
		 * 返回上一页
		 */
		function _go_back($n='-1'){
			echo "<script type='text/javascript'>history.back(".$n.")</script>";
		}
		/*
		 * 提示消息
		 */
		function _show_message($message){
			echo "<script type='text/javascript'>alert('".$message."');</script>";

		}
}




?>
